package com.ojoin.trade.common_core.web.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ojoin.trade.common.exception.DaoException;
import com.ojoin.trade.common.utils.BeanMapUtils;

import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.web.dao.impl.BaseDaoImpl;
import com.ojoin.trade.common_core.web.dao.TbMerchantInfoDao;
import com.ojoin.trade.common_core.web.domain.TbMerchantInfo;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 15-11-16
 * Time: 下午6:25
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class TbMerchantInfoDaoImpl extends BaseDaoImpl<TbMerchantInfo> implements TbMerchantInfoDao {


    @Override
    public void updateBySelective(TbMerchantInfo query) throws BusinessException {
        String mapMethodName = "update";
        try {
        	Map<String, Object> params = BeanMapUtils.toMap(query);
             sqlSessionTemplate.update(
                    getSqlName(mapMethodName),
                    params);
        } catch (Exception e) {
            throw new DaoException(String.format("更新对象列表出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

    @Override
    public TbMerchantInfo queryInfoByJoinid(int idOfTbMerchantUser) throws BusinessException {
        String mapMethodName = "selectByJoinid";
        try {
           return sqlSessionTemplate.selectOne(
                    getSqlName(mapMethodName),
                    idOfTbMerchantUser);
        } catch (Exception e) {
            throw new DaoException(String.format("查询对象列表出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

    @Override
    public List<TbMerchantInfo> queryAllInfo(Pageable page) throws BusinessException {
        String mapMethodName = "selectAll";
        try {
         return    sqlSessionTemplate.selectList(getSqlName(mapMethodName), this.getPageBounds(page));
        } catch (Exception e) {
            throw new DaoException(String.format("查询对象列表出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

	@Override
	public TbMerchantInfo selectRealnameInfoByJoinid(int joinid)
			throws BusinessException {
		String mapMethodName = "selectRealnameInfoByJoinid";
        try {
           return sqlSessionTemplate.selectOne(
                    getSqlName(mapMethodName),
                    joinid);
        } catch (Exception e) {
            throw new DaoException(String.format("查询joinuserid" + joinid + "的实名信息出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
	}

	@Override
	public int updateRealStatusByJoinuserId(int joinuserid, int realStatus)
			throws BusinessException {
		String mapMethodName = "updateRealStatusByJoinuserId";
        try {
        	HashMap<String, Object> map = new HashMap<String, Object>();
        	map.put("joinuserid", joinuserid);
//        	map.put("realStatus", realStatus);
            return sqlSessionTemplate.update(
                    getSqlName(mapMethodName),
                    map);
        } catch (Exception e) {
            throw new DaoException(String.format("更新对象列表出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
	}

	@Override
	public int updateMhtnoByJoinuserId(int joinuserid, String mhtno)
			throws BusinessException {
		String mapMethodName = "updateMhtnoByJoinuserId";
        try {
        	HashMap<String, Object> map = new HashMap<String, Object>();
        	map.put("joinuserid", joinuserid);
        	map.put("mhtno", mhtno);
            return sqlSessionTemplate.update(
                    getSqlName(mapMethodName),
                    map);
        } catch (Exception e) {
            throw new DaoException(String.format("更新对象列表出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
	}

	@Override
	public List<TbMerchantInfo> queryByNameVague(String name) {
		String mapMethodName = "selectByNameVague";
        try {
        	HashMap<String, Object> map = new HashMap<String, Object>();
        	map.put("limit", 10);
        	map.put("mchName", "%"+name+"%");
        	
        	
         return    sqlSessionTemplate.selectList(getSqlName(mapMethodName),map);
        } catch (Exception e) {
            throw new DaoException(String.format("查询对象列表出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
	}

	@Override
	public TbMerchantInfo queryInfoByMhtno(String mhtno)
			throws BusinessException {
		String mapMethodName = "queryInfoByMhtno";
        try {
           return sqlSessionTemplate.selectOne(
                    getSqlName(mapMethodName),
                    mhtno);
        } catch (Exception e) {
            throw new DaoException(String.format("查询对象列表出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
	}
}
